/**
* @file init_callback_register.cpp
*
* Copyright (c) Huawei Technologies Co., Ltd. 2025. All Rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "runtime/acl_rt_impl.h"
#include "common/common_inner.h"

#ifdef __cplusplus
extern "C" {
#endif
extern __attribute__((weak, visibility("default"))) void conflicting_func_ascendcl();
#ifdef __cplusplus
}
#endif

namespace acl {
GeFinalizeCallback aclGeFinalizeCallback = nullptr;
GeFinalizeCallback GetGeFinalizeCallback()
{
    return aclGeFinalizeCallback;
}

void SetGeFinalizeCallback(const GeFinalizeCallback func)
{
    aclGeFinalizeCallback = func;
}

aclError AclOpCompilerFinalizeCallbackFunc(void *userData)
{
    (void)userData;
    ACL_LOG_INFO("start to enter AclOpCompilerFinalizeCallbackFunc");
    auto geFinalizeCbFunc = acl::GetGeFinalizeCallback();
    if (geFinalizeCbFunc != nullptr) {
        const auto ret = geFinalizeCbFunc();
        if (ret != ACL_SUCCESS) {
            ACL_LOG_INNER_ERROR("[Finalize][Ge]ge finalize failed, errorCode = %d", static_cast<int32_t>(ret));
        }
    }
    return ACL_SUCCESS;
}
__attribute__((constructor)) aclError RegAclOpCompilerFinalizeCallback()
{
    return aclFinalizeCallbackRegisterImpl(ACL_REG_TYPE_ACL_OP_COMPILER,
                                           AclOpCompilerFinalizeCallbackFunc, nullptr);
}
__attribute__((destructor)) aclError UnRegAclOpCompilerFinalizeCallback()
{
    if (conflicting_func_ascendcl == nullptr) {
        return aclFinalizeCallbackUnRegisterImpl(ACL_REG_TYPE_ACL_OP_COMPILER,
                                                 AclOpCompilerFinalizeCallbackFunc);
    }
    return ACL_SUCCESS;
}
}